package command

import (
	"fmt"
)

func NewProgressBar(total, length int, displayChar string) *ProgressBar {
	return &ProgressBar{
		total:       total,
		current:     0,
		length:      length,      // 长度
		displayChar: displayChar, // 展示字符
	}
}

type ProgressBar struct {
	total       int
	current     int
	length      int
	displayChar string
}

func (r *ProgressBar) Update(current int) {
	r.current = current

	percent := float64(r.current) / float64(r.total)
	filledLength := int(percent * float64(r.length))
	bar := ""
	for i := 0; i < filledLength; i++ {
		bar += r.displayChar
	}
	for i := filledLength; i < r.length; i++ {
		bar += " "
	}
	fmt.Printf("\r[%s] %d%%", bar, int(percent*100))
}
